package com.IO.JieYaSuoLiu;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class A02 {
    public static void main(String[] args) throws IOException {
        /*
        * 把文件打包成压缩包
        * */
        File src = new File("D:\\TestFile\\zip\\test.txt");
        File dest = new File("D:\\TestFile\\zip");
        zipFile(src,dest);
    }
    
    /*
    * 参数一：表示压缩路径
    * 参数二：压缩包地址
    * */
    public static void zipFile(File src, File dest) throws IOException {
        // 压缩流关联压缩包
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(dest, "test.zip")));
        // 创建zipentry对象
        ZipEntry entry = new ZipEntry("test.txt");
        // 把zipentry放到压缩包中
        zos.putNextEntry(entry);
        // 把src文件夹中的数据写到压缩包
        FileInputStream fis = new FileInputStream(src);
        int b;
        while ((b = fis.read()) != -1) {
            zos.write(b);
        }
        zos.closeEntry();
        zos.close();
    }
}
